package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.dubbo.mapper.SmsCouponMapper;
import com.mall.model.domain.SmsCoupon;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author xyiaao
 * @description 针对表【sms_coupon(优惠券表)】的数据库操作Api实现
 * @createDate 2022-03-29 19:46:09
 */
@DubboService
public class SmsCouponApiImpl extends ServiceImpl<SmsCouponMapper, SmsCoupon>
        implements SmsCouponApi {

    @Autowired
    private SmsCouponMapper smsCouponMapper;

    //根据优惠券名称和类型分页获取优惠券列表
    public Page<SmsCoupon> listPage(Integer pageNum, Integer pageSize, String name, Integer type) {

        LambdaQueryWrapper<SmsCoupon> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), SmsCoupon::getName, name);
        lqw.eq(type != null, SmsCoupon::getType, type);

        Page<SmsCoupon> page = new Page<>(pageNum, pageSize);

        return smsCouponMapper.selectPage(page, lqw);
    }

    @Override
    public void updateCoupon(Integer id, SmsCoupon smsCoupon) {

        LambdaQueryWrapper<SmsCoupon> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null, SmsCoupon::getId, id);
        smsCouponMapper.update(smsCoupon, lqw);
    }

    @Override
    public Long saveCoupon(SmsCoupon smsCoupon) {

        smsCouponMapper.insert(smsCoupon);

        return smsCoupon.getId();
    }

}




